package com.example.mystoryapp2.data.network.model

import com.example.mystoryapp2.data.network.NetCallBack
import com.example.mystoryapp2.data.network.ServiceCreator
import com.example.mystoryapp2.data.network.service.StoryOperationService
import com.example.mystoryapp2.domain.Story
import retrofit2.Call
import retrofit2.Response

object StoryOperationModel {
    private val TAG = "StoryOperationModel"
    private var storyOpeService = ServiceCreator.create<StoryOperationService>()

    fun collect(story: Story, callBack: ServiceCreator.ICallBack<Boolean>) {
        storyOpeService.collect(story).enqueue(object : NetCallBack<Boolean>() {
            override fun onResponse(call: Call<Boolean>, response: Response<Boolean>) {
                val body = response.body()
                if (body != null) {
                    callBack.onSuccess(body)
                } else {
                    callBack.onSuccess(false)
                }
            }

            override fun onFailure(call: Call<Boolean>, t: Throwable) {
                callBack.onSuccess(false)
            }
        })
    }

    fun delete(story: Story, callBack: ServiceCreator.ICallBack<Boolean>) {
        storyOpeService.delete(story).enqueue(object : NetCallBack<Boolean>() {
            override fun onResponse(call: Call<Boolean>, response: Response<Boolean>) {
                val body = response.body()
                if (body != null) {
                    callBack.onSuccess(body)
                } else {
                    callBack.onSuccess(false)
                }
            }

            override fun onFailure(call: Call<Boolean>, t: Throwable) {
                callBack.onSuccess(false)
            }
        })
    }

    fun publish(story: Story, callBack: ServiceCreator.ICallBack<Boolean>) {
        storyOpeService.publish(story).enqueue(object : NetCallBack<Boolean>() {
            override fun onResponse(call: Call<Boolean>, response: Response<Boolean>) {
                val body = response.body()
                if (body != null) {
                    callBack.onSuccess(body)
                } else {
                    callBack.onSuccess(false)
                }
            }

            override fun onFailure(call: Call<Boolean>, t: Throwable) {
                callBack.onSuccess(false)
            }
        })
    }

    fun checkCollect(story: Story, callBack: ServiceCreator.ICallBack<Boolean>) {
        storyOpeService.checkCollect(story).enqueue(object : NetCallBack<Boolean>() {
            override fun onResponse(call: Call<Boolean>, response: Response<Boolean>) {
                val body = response.body()
                if (body != null) {
                    callBack.onSuccess(body)
                } else {
                    callBack.onSuccess(false)
                }
            }

            override fun onFailure(call: Call<Boolean>, t: Throwable) {
                callBack.onSuccess(false)
            }
        })
    }
}